<!--
 * @Description: 
 * @Author: 胡涛
 * @Date: 2023-09-11 21:21:30
 * @LastEditors: 胡涛
 * @LastEditTime: 2023-10-24 10:12:08
-->
<template>
  <div class="page-wrapper">
    <div class="toolbar">
      <div class="breadcrumb">
        <a-breadcrumb>
          <a-breadcrumb-item>组织架构</a-breadcrumb-item>
          <a-breadcrumb-item>企业信息管理</a-breadcrumb-item>
        </a-breadcrumb>
      </div>
      <a-space>
        <a-button type="primary" @click="onHandleCreate" v-auth="'Create'">
          <template #icon>
            <PlusOutlined />
          </template>
          添加
        </a-button>
        <a-button type="primary" :loading="loading" @click="queryList">
          <template #icon>
            <SyncOutlined />
          </template>
          刷新
        </a-button>
        <!-- <a-button type="primary">
          <template #icon>
            <ExportOutlined />
          </template>
          导出
        </a-button> -->
      </a-space>
    </div>
    <div class="container">
      <div class="table">
        <a-table
          bordered
          sticky
          :columns="columns"
          :row-key="(record) => record.id"
          :data-source="dataSource"
          :pagination="pagination"
          :loading="loading"
          @change="handleTableChange"
        >
        </a-table>
      </div>
    </div>
  </div>
  <EditView
    v-model:visible="popupModel.visible"
    :action="popupModel.action"
    :id="popupModel.id"
    v-if="popupModel.visible"
    @success="queryList"
  />
</template>

<script lang="ts" setup>
import {
  PlusOutlined,
  ExportOutlined,
  SyncOutlined,
  EditOutlined,
  DeleteOutlined,
} from "@ant-design/icons-vue";
import { useListPage } from "@/helpers/pages";
import api from "@/api/organize/enterprise";
import { EnterpriseDto } from "@/api/organize/types";
import { h, onMounted, ref } from "vue";
import { PaginationConfig } from "ant-design-vue/es/pagination";
import { enumName, enums } from "@/plugins/enums";
import { checkActionStyle } from "@/helpers";

import {
  Button,
  ButtonGroup,
  message,
  Popconfirm,
  Switch,
} from "ant-design-vue";
import EditView from "./Edit.vue";
import { dictionaryName, dictionaryKeys } from "@/plugins/dictionary";
import { getIconComponent } from "@/helpers";
import { ITreeOptionItem } from "@/types";
const {
  loading,
  dataSource,
  columns,
  pagination,
  popupModel,
  createQueryModel,
  onHandleCreate,
  onHandleUpdate,
} = useListPage<EnterpriseDto, string>({
  columns: [
    { dataIndex: "code", title: "编码", width: 150 },
    { dataIndex: "name", title: "企业名称" },
    { dataIndex: "type", title: "行业类型", width: 100 },
    { dataIndex: "subType", title: "子类型", width: 100 },
    { dataIndex: "businessCode", title: "企业代码", width: 200 },
    { dataIndex: "address", title: "企业地址" },
    // { dataIndex: "phone", title: "联系电话", width: 150 },
    // { dataIndex: "description", title: "企业简介" },
    {
      dataIndex: "status",
      title: "启用状态",
      align: "center",
      customRender({ value }) {
        return h(Switch, { size: "small", disabled: true, checked: value });
      },
    },
    {
      dataIndex: "action",
      title: "操作",
      width: 150,
      customRender: ({ record }: any) => {
        return h(ButtonGroup, [
          h(
            Button,
            {
              type: "primary",
              size: "small",
              ghost: true,
              style: checkActionStyle("Update"),
              onClick() {
                onHandleUpdate(record);
              },
            },
            {
              icon() {
                return [h(EditOutlined)];
              },
              default: () => {
                return "修改";
              },
            }
          ),
          h(
            Popconfirm,
            {
              title: "删除提示",
              description: "确定要删除当前选择行数据信息吗？",
              onConfirm: () => {
                onDeleteItem(record);
              },
            },
            {
              default: () => {
                return h(
                  Button,
                  {
                    style: checkActionStyle("Delete"),
                    danger: true,
                    size: "small",
                  },
                  {
                    icon() {
                      return [h(DeleteOutlined)];
                    },
                    default: () => {
                      return "删除";
                    },
                  }
                );
              },
            }
          ),
        ]);
      },
    },
  ],
});
const onDeleteItem = async (item: EnterpriseDto) => {
  let callback = message.loading("数据操作中，请稍后");
  await api.delete(item.id);
  callback();
  await queryList();
};

const queryList = async () => {
  loading.value = true;
  let result = await api.getList(createQueryModel());
  dataSource.value = result.items;
  pagination.total = result.totalCount;
  loading.value = false;
};

const handleTableChange = (pageConfig: PaginationConfig, filters: any) => {
  pagination.current = pageConfig.current;
  queryList();
};

onMounted(() => {
  queryList();
});
</script>

<style lang="less" scoped></style>
